//
//  SEGMessageLikeViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/10/15.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGMessageLikeViewController.h"
#import "MessageLikeTableViewCell.h"

#import "SEGMessageLikeViewModel.h"

@interface SEGMessageLikeViewController () <UITableViewDelegate, UITableViewDataSource, LikeCellDelegate>

/// 表格
@property (nonatomic, strong) UITableView *tableView;
/// viewModel
@property (nonatomic, strong) SEGMessageLikeViewModel *viewModel;

@end

@implementation SEGMessageLikeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showCustomNavigationBackButton];
    // 订阅信号
    @weakify(self);
    [[self.viewModel.command execute:self.groupId] subscribeNext:^(NSArray *data) {
        @strongify(self);
       if (data.count > 0) {
           [[SEGUhomeMediator sharedInstance] seg_hideCustomProgressImage:@{@"view":self.tableView}];
           [self.tableView reloadData];
       } else {
           [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":self.tableView}];
       }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    MessageLikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MessageModel *model = self.viewModel.dataArray[indexPath.row];
    [cell showWithModel:model];
    if (self.viewModel.userInfoDict && [self.viewModel.userInfoDict valueForKey:model.sendUserId]) {
         [cell showUserInfoWithModel:self.viewModel.userInfoDict[model.sendUserId]];
    }
    cell.delegate = self;
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return 97;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return UITableViewAutomaticDimension;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    MessageModel *msgModel = self.viewModel.dataArray[indexPath.row];
    [self didTapSubMessageWithModel:msgModel];
}

#pragma mark - LikeCellDelegate
- (void)didTapSubMessageWithModel:(MessageModel *)msgModel {
    @weakify(self);
    [self.viewModel seg_pushDetailVCWithModel:msgModel successed:^(NSString *type, NSString *parentObjType, NSDictionary *params) {
        @strongify(self);
        if ([type isEqualToString:kMessageTypePictorial]) { // PGC详情
            UIViewController *vc = [[SEGUhomeMediator sharedInstance] seg_initPGCDeteailViewController:params];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        } else { // 活动评论详情
            if ([parentObjType integerValue] == 6) {
                UIViewController *activityCommentVC = [[SEGUhomeMediator sharedInstance] seg_initActivityCommonDetailViewController:params];
                [self.navigationController pushViewController:activityCommentVC animated:YES];
                return;
            }
            // 邻里圈详情
            UIViewController *vc = [[SEGUhomeMediator sharedInstance] seg_initNeihborhoodDetailViewController:params];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } error:^(NSString *errorMsg) {
        [SEGAlertView showTmepMessageWithMessage:errorMsg];
    }];
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = kColor_bg;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [_tableView registerClass:[MessageLikeTableViewCell class] forCellReuseIdentifier:@"cellId"];
        [self.view addSubview:_tableView];
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); //设置端距
    }
    return _tableView;
}
- (SEGMessageLikeViewModel *)viewModel {
    if (!_viewModel) _viewModel = [[SEGMessageLikeViewModel alloc] init];
    return _viewModel;
}

@end
